package com.learn;

import com.learn.entity.Student;
import com.learn.mul.MoreThread;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author summer
 * @date 2022-05-10  18:56
 */
public class Main {
    public static void main(String[] args) {
        List<Student> dataList = new ArrayList<>();
        for (int i = 0; i < 100002; i++) {
            Student student = new Student("张三" + i, "男", i, "99999");
            dataList.add(student);
        }
        try {
//            exportData(dataList); 28705
            exportDataMore(dataList, 4);//16037
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void exportData(List<Student> dataList) throws IllegalAccessException, IOException {
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        SXSSFSheet sheet = workbook.createSheet();
        sheet.setRandomAccessWindowSize(-1);

        Iterator<Student> iterator = dataList.iterator();
        Field[] fields = Student.class.getDeclaredFields();

        int currentRow = 0;
        while (iterator.hasNext()) {
            Student student = null;
            SXSSFRow row = sheet.createRow(currentRow);

            if (currentRow != 0) {
                student = iterator.next();
            }
            for (int i = 0; i < fields.length; i++) {
                SXSSFCell cell = row.createCell(i);

                if (currentRow == 0) {
                    cell.setCellValue(fields[i].getName());
                } else {
                    Field field = fields[i];
                    field.setAccessible(true);
                    cell.setCellValue(String.valueOf(field.get(student)));
                }
            }
            currentRow++;

        }

        FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\Summer\\Desktop\\poi\\student.xlsx"));

        workbook.write(fos);

    }

    public static void exportDataMore(List<Student> studentList, int threadNum) throws Exception {
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        // XSSFWorkbook workbook = new XSSFWorkbook();

        int count = studentList.size();
        int avgCount = (int) Math.floor(count / threadNum);
        int modeCount = count % threadNum;

        int start = 0;
        int end = 0;
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        for (int i = 0; i < threadNum; i++) {
            start = i * avgCount;
            if (i == threadNum - 1) {
                end = (i + 1) * avgCount + modeCount;
            } else {
                end = (i + 1) * avgCount;
            }
            List<Student> students = studentList.subList(start, end);
            Sheet sheet = workbook.createSheet();

            new Thread(new MoreThread(students, sheet, countDownLatch), "线程：" + i).start();
        }
        countDownLatch.await();
        FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\Summer\\Desktop\\poi\\student-more.xlsx"));

        workbook.write(fos);
        System.out.println("导出完成");
    }

}
